var tgd = tgd || {};
tgd.Utilities = tgd.Utilities || {};
tgd.Utilities.Calendar = tgd.Utilities.Calendar || {};
tgd.Utilities.Calendar.Views = tgd.Utilities.Calendar.Views || {};

tgd.Utilities.Calendar.Views.HTMLMonth = function (canvas, month) {
	this._canvas = canvas;
	this._month = month;
	this._tbl = null;

	var row = null;
	var cell = null;

	if (tgd.Utilities.Calendar.Views.HTMLMonth._initialized === undefined) {

		tgd.Utilities.Calendar.Views.HTMLMonth.prototype._buildTable = function() {
			this._tbl = document.createElement("table");
			/* MONTH AND YEAR HEADER */
			row = this._tbl.insertRow(0);
			/* MONTH CELL */
			var cell = row.insertCell(0);
			cell.colSpan = 4;
			cell.style.textAlign = "left";
			/* YEAR CELL */
			cell = row.insertCell(1);
			cell.colSpan = 3;
			cell.style.textAlign = "right";

			/* WEEK DAYS ABBREVIATIONS */
			row = this._tbl.insertRow(1);
			row.className = "weekdays";
			for (var i = 0; i < 7; ++i) {
				cell = row.insertCell(i);
				cell.innerHTML = tgd.Utilities.Calendar.DayNames.getDayAbbreviation(i);
			}

			/* DAYS OF THE MONTH 5 ROWS 7 COLUMNS */
			for (var i = 0; i < 6; ++i) {  //5 rows in the calendar
				row = this._tbl.insertRow(i + 2);
				for (var j = 0; j < 7; ++j) {  //7 days per week
					cell = row.insertCell(j);
					cell.innerHTML = "&nbsp;";
				}
			}
		}

		tgd.Utilities.Calendar.Views.HTMLMonth.prototype._setTableValues = function() {
			var isCurrentMonth = (this._month.getYear() == new Date().getFullYear() && this._month.getMonth() == new Date().getMonth());
			var lastMonth = new tgd.Utilities.Calendar.Month(this._month.getYear() - ((this._month.getMonth() + 11) % 12 == 11 ? 1 : 0), (this._month.getMonth() + 11) % 12);

			/* YEAR AND MONTH HEADING */
			this._tbl.rows[0].cells[0].innerHTML = this._month.getMonthName();
			this._tbl.rows[0].cells[1].innerHTML = this._month.getYear();

			/* DAYS OF THE MONTH */
			var cur = 0;
			for (var i = 0; i < 6; ++i) {  //5 rows in the calendar
				row = this._tbl.rows[i + 2];
				for (var j = 0; j < 7; ++j) {  //7 days per week
					cell = row.cells[j];
					if (cur === 0 && j === this._month.getFirstDayOfWeek()) {
						++cur;
					}
						
					if (cur === 0) {
						cell.innerHTML = lastMonth.getNumberOfDaysInMonth() - this._month.getFirstDayOfWeek() + j + 1;
						cell.className = "previousmonth";
					} else if (cur > this._month.getNumberOfDaysInMonth()) {
						cell.innerHTML = cur - this._month.getNumberOfDaysInMonth();
						cell.className = "nextmonth";
						++cur;
					} else {
						cell.className = (isCurrentMonth && cur == new Date().getDate() ? "today" : "");
						cell.innerHTML = cur
						++cur;
					}
				}
			}
		}

		tgd.Utilities.Calendar.Views.HTMLMonth.prototype.setMonth = function(month) {
			this._month = month;
			this._setTableValues();
		}

		tgd.Utilities.Calendar.Views.HTMLMonth.prototype.hideMonthYear = function() {
			this._tbl.rows[0].style.display = "none";
		}

		tgd.Utilities.Calendar.Views.HTMLMonth._initialized = true;
	}

	this._buildTable();
	this._setTableValues();

	/* ADD TABLE TO CANVAS */
	this._canvas.appendChild(this._tbl);
}